/**
 * Copyright (C) 2008 University of Pittsburgh
 * 
 * 
 * This file is part of Open EpiCenter
 * 
 *     Open EpiCenter is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     Open EpiCenter is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 * 
 *     You should have received a copy of the GNU General Public License
 *     along with Open EpiCenter.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * 
 *   
 */
package com.hmsinc.epicenter.model.health;

// Generated Jan 15, 2007 10:58:01 AM by Hibernate Tools 3.2.0.beta8

import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.hibernate.annotations.ForeignKey;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import com.hmsinc.epicenter.model.analysis.classify.Classification;
import com.hmsinc.epicenter.model.attribute.PatientClass;
import com.hmsinc.epicenter.util.FormatUtils;

/**
 * Registration generated by hbm2java
 */
@Entity
@Table(name = "REGISTRATION")
@Inheritance(strategy = InheritanceType.JOINED)
@NamedQueries( {
		@NamedQuery(name = "findExistingNaturalKey", query = "select messageId from Registration a where naturalKey = :naturalKey"),
		@NamedQuery(name = "getInteractions", query = "from Registration h join h.classifications c join h.patient p join h.patientDetail pd where h.id in (:interactions)") })
@ForeignKey(name = "FK_REGISTRATION_1")
public class Registration extends Interaction implements CodedVisit {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6509106309476525228L;

	private static final DateTimeFormatter KEY_FORMAT = DateTimeFormat.forPattern("yyyyMMddHHmmss");
	
	private String visitNumber;

	private String naturalKey;

	private String reason;

	private String icd9;

	/**
	 * 
	 */
	public Registration() {
		super();
	}

	/**
	 * @param interactionType
	 */
	public Registration(PatientClass patientClass) {
		super(patientClass);
	}

	/**
	 * @param id
	 * @param patient
	 * @param patientDetail
	 * @param messageId
	 * @param interactionDate
	 * @param classifications
	 */
	public Registration(Long id, Patient patient, PatientDetail patientDetail, Long messageId, DateTime interactionDate,
			Set<Classification> classifications) {
		super(id, patient, patientDetail, messageId, interactionDate, classifications);
	}

	/**
	 * @param id
	 */
	public Registration(Long id) {
		super(id);
		// TODO Auto-generated constructor stub
	}

	@Column(name = "VISIT_NUM", unique = false, nullable = true, insertable = true, updatable = true, length = 40)
	public String getVisitNumber() {
		return this.visitNumber;
	}

	public void setVisitNumber(String visitNumber) {
		this.visitNumber = visitNumber;
	}

	@Column(name = "NATURAL_KEY", unique = true, nullable = false, insertable = true, updatable = true, length = 400)
	public String getNaturalKey() {
		return this.naturalKey == null ? generateNaturalKey() : this.naturalKey;
	}

	public void setNaturalKey(String naturalKey) {
		this.naturalKey = naturalKey;
	}

	@Column(name = "REASON", unique = false, nullable = true, insertable = true, updatable = true, length = 400)
	public String getReason() {
		return this.reason;
	}

	public void setReason(String reason) {
		this.reason = reason;
	}

	@Column(name = "ICD9", unique = false, nullable = true, insertable = true, updatable = true, length = 80)
	public String getIcd9() {
		return this.icd9;
	}

	public void setIcd9(String icd9) {
		this.icd9 = icd9;
	}

	private String generateNaturalKey() {

		final String key;

		// Use patientID + visitNumber + facilityName if possible:
		if (getPatient() != null && getPatient().getFacility() != null && getPatient().getPatientId() != null && getVisitNumber() != null) {
			
			key = FormatUtils.join("|", getPatient().getFacility().getId(), getPatientClass().getAbbreviation(), getPatient().getPatientId(), getVisitNumber());

		} else if (getPatient().getFacility() != null && getPatient() != null && getPatientDetail() != null) {
			
			key = FormatUtils.join("|", getPatient().getFacility().getId(), getPatientClass().getAbbreviation(), getPatient().getPatientId(),
				(getInteractionDate() != null ? KEY_FORMAT.print(getInteractionDate()) : null),
				getReason(), getPatientDetail().getZipcode(), getAgeAtInteraction(), (getPatientDetail().getGender() == null ? null : getPatientDetail().getGender().getAbbreviation()));

		} else {
			key = null;
		}
		return key;
	}

	@Override
	public String toString() {
		return new ToStringBuilder(this).appendSuper(super.toString()).append("visitNumber", visitNumber).append(
				"reason", reason).append("icd9", icd9).append("naturalKey", naturalKey).toString();
	}
}
